package com.cskaoyan.service.market;

import com.cskaoyan.bean.market.Brand;
import com.cskaoyan.bean.market.BrandExample;
import com.cskaoyan.bo.market.BrandCreateBo;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.utils.Converter;
import com.cskaoyan.utils.PojoUtils;
import com.cskaoyan.vo.market.BrandCreateVo;
import com.cskaoyan.vo.market.BrandDataVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-09 17:29
 **/


// 品牌制造商
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;

    // admin/brand/list?page=1&limit=20&sort=add_time&order=desc
    @Override
    public BrandDataVo brandList(Integer page, Integer limit, String sort, String order, Integer id, String name) {

        PageHelper.startPage(page, limit);
        // 查询，获取数据
        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause(sort + " " + order);
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        // 条件查询
        if (id != null && !"".equals(id)) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        List<Brand> brands = brandMapper.selectByExample(brandExample);

        // 类型转换
        ArrayList<BrandDataVo.ItemsBean> list = new ArrayList<>();
        Converter converter = PojoUtils.converter();
        converter.listConverter(brands,list,BrandDataVo.ItemsBean.class);

        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        int total = (int) pageInfo.getTotal();

        return new BrandDataVo(total, list);
    }

    // http://localhost:8083/admin/brand/update
    @Override
    public Brand brandUpdate(Brand brand) {
        brand.setUpdateTime(new Date());
        brandMapper.updateByPrimaryKey(brand);
        Brand brandVo = brandMapper.selectByPrimaryKey(brand.getId());
        return brandVo;
    }

    // http://localhost:8083/admin/brand/delete
    @Override
    public Brand brandDelete(Brand brand) {
        brand.setDeleted(true);
        brand.setUpdateTime(new Date());
        brandMapper.updateByPrimaryKey(brand);
        return brand;
    }

    @Override
    public BrandCreateVo createBrand(BrandCreateBo brandCreateBo) {
        // 参数类型转换
        Brand brand = new Brand();
        Converter converter = PojoUtils.converter();
        converter.converter(brandCreateBo,brand);
        // 添加数据操作
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brand.setDeleted(false);
        brandMapper.insertSelective(brand);

        // 参数类型转化
        BrandCreateVo brandCreateVo = new BrandCreateVo();
        Converter converter1 = PojoUtils.converter();
        converter1.converter(brand,brandCreateVo);

        return brandCreateVo;
    }

    @Override
    public Brand wxBrandQuery(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    @Override
    public Map<String, Object> wxBrandListQuery(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        HashMap<String, Object> map = new HashMap<>();
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        long total = new PageInfo<>(brands).getTotal();
        map.put("brandList",brands);
        long totalPages = (total / size ) + 1;
        map.put("totalPages",totalPages);
        //map  put list<> brandList  put  totalPages
        return map;
    }
}
